/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-11-24
 * Time: 23:03
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;

using Labb4.Pooman.Cards;
using Labb4.Pooman.Players;

namespace Labb4.Pooman.Game
{
	/// <summary>
	/// The state of game phase 1
	/// 
	/// Implements the State design pattern
	/// </summary>
	public class GameStatePhase1 : GameState
	{
		#region constructors
		
		public GameStatePhase1(PoomanMain poo) :
			base(poo)
		{
		}
		
		#endregion
		
		#region overrides
		
		/// <summary>
		/// If a stick has not been able to finish due to the players being all out of cards,
		/// the remaining cards will ha ve to go to a player. This method take scare of that action
		/// </summary>
		public override void LastActionOfThePhase()
		{
			//TODO find out who really should have the final cards on the table..
			//AiValuePlayer.CURRENT_HAND.CARDS.Add(OneStick.GetAllCards());
			//TODO if the player with fewest cards on the hand for phase 2 has less than 5 cards,
			//		give this player all cards below 5 from the other players
			//			then play appropriate event for this
			//PlayerManager.Instance[1].CURRENT_HAND.CARDS.Add(OneStick.GetAllCards());
			Table.Instance.GetCards(PlayerManager.Instance[1]);
		}
		
		/// <summary>
		/// Collects cards from all players and determines who has played the highest card. If more
		/// than one player has played the highest card, these players go again, until there is one
		/// player left who has played the highest card in one round
		/// </summary>
		/// <param name="deltaTime">The time passed since the last time this method was called</param>
		public override void PlayNextStick(int deltaTime)
		{
			if(!m_FoundWinnerOfLastStick)
			{
				if(m_Stick.GetWinner(ref m_Swarm))
				{
					m_FoundWinnerOfLastStick = true;
				}
			}
			else
			{
				m_CounterForDisplayingWonCards += deltaTime;
				
				if(m_CounterForDisplayingWonCards > Properties.Instance.DisplayTimeForWonCards)
				{
					m_CounterForDisplayingWonCards = 0;
					m_FoundWinnerOfLastStick = false;
					
					Player winner = m_Stick.Winner;
					m_Swarm.Clear(); //TODO why? it should be empty..
					
					//CardPile wonCards = OneStick.GetAllCards();
					//winner.TakeCards(wonCards);
					//Table.Instance.CARDS_ON_TABLE.Clear();
					Table.Instance.GetCards(winner);
					m_Stick.Clear(); //TODO why?
					
					m_PlayerManager.GetNextSwarm(winner, m_Swarm);
					
					if(m_Swarm.Count != m_PlayerManager.Count)
					{
						//not all players were added to the swarm
						//this could only mean that there are players with no cards
						//in this phase of the game, this means that the phase is over..
						m_PoomanMain.SwitchPhase();
					}
				}
			}
		}
		
		/// <summary>
		/// Resets this phase to its start values
		/// </summary>
		/// <param name="firstPlayerInSwarm">A reference to the player who should be first to act in this phase</param>
		public override void ResetPhase(Player firstPlayerInSwarm)
		{
			base.ResetPhase(firstPlayerInSwarm);
			
			//deal 3 cards to each player
			int interval = 600;
			int delay = 0;
			
			for(int i = 0; i < 3; i++)
			{
				for(int j = 0; j < m_PlayerManager.Count; j++)
				{
					delay += interval;
					m_PoomanMain.DealOneCardToPlayer(m_PlayerManager[j], delay);
				}
			}
			
			m_PlayerManager.GetNextSwarm(firstPlayerInSwarm, m_Swarm);
		}
		
		#endregion
	}
}
